<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('jy_kaoshi_video_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_kaoshi_video',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['groupid']]['title'];			
			$item['url']=custom_url('jy_kaoshi/video.detail//id='.$item['id'],true);
		}
	}
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['list']=$list?$list:array();
	$return['fenlei']=$fenlei?$fenlei:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'视频列表'),
			array('op'=>'edit','title'=>'添加视频'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'视频标题','type'=>'string'),
			array('name'=>'fenlei','title'=>'视频分类','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			//array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			//array('name'=>'likenum','title'=>'点赞量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'seo_send','title'=>'一键推送','type'=>'modal'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='seo_send'){
	$return['item']['id']=intval($_RQ['id']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'一键推送文章',
		'edit'=>array(
			array('type'=>'tip','title'=>'<red>【温馨提示】：需要移步站点宣传--多媒体--多媒体设置。设置好后，这里才能推送视频！</red>'),
			array('type'=>'switch','title'=>'抖音号推送','name'=>'jy_douyin'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_kaoshi_video',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	$video=pdo_get('jy_kaoshi_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$video['content'] = strip_tags(str_replace('&nbsp;','',$video['content']));
	$video['desc'] = $video['desc'] ? $video['desc'] : cfc('tools')->substr($video['content'],0,36);
	$video['url'] =custom_url('jy_kaoshi/video.detail//id='.$video['id'],true);
	if($post['jy_douyin'] && check_permission('jy_douyin')){
		model('jy_douyin/set_data')->upvideo($video['video']);
	}
	exi('发布成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_kaoshi_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_kaoshi_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kaoshi_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post2',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回视频列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'名称','type'=>'string','must'=>true),
					array('name'=>'image','title'=>'logo','type'=>'image'),
					array('name'=>'desc','title'=>'描述','type'=>'text'),
					array('name'=>'groupid','title'=>'分类','type'=>'select','options'=>'groups'),
					array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_kaoshi/goods.get_goods/','choose_title'=>'选择商品','help'=>'用于商品关联'),
					array('type'=>'choose','title'=>'选择用户','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择用户'),
					array('name'=>'video','title'=>'短视频','type'=>'video'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_kaoshi_video_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post2'){
	$postdata=get_postdata();
	$fields=array('title','image','desc','goodsid','mid','video','groupid');
	$data=array_fields($fields,$postdata);
	if(empty($data['title'])){
		exi('请填写视频标题！','error');
	}
	if(empty($data['groupid'])){
		exi('请填写视频分类！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kaoshi_video',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑视频成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kaoshi_video',$data);
		$id=pdo_insertid();
		exi('添加视频成功！','','list');
	}
}

